﻿using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using System.Globalization;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Windows.Data;

namespace SUNRPA.CONTROLS.IMPL.domain {

    public class RelationConvertion:IValueConverter { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            return Enum.Parse(typeof(ConditionRelations), value.ToString());
        }
    }

    public enum ConditionRelations { 
        EQUAL,
        NOT_EQUAL,
        GREATER,
        LESS,
        //大于等于
        EOG,
        //小于等于
        EOL,
        CONTAINS,
        NOT_CONTAIONS,
        IS_TRUE,
        IS_FALSE,
        IS_NULL,
        NOT_NULL,
        IS_EMPTY_STRING,
        NOT_EMPTY_STRING,
        START_WITH,
        END_WITH,
        NOT_START_WITH,
        NOT_END_WITH
    }
    public class IFCondition:BaseDomain{
        
        public static SortedDictionary<ConditionRelations, string> relationAndName = new SortedDictionary<ConditionRelations, string>() {
            {ConditionRelations.EQUAL, "等于"},
            {ConditionRelations.NOT_EQUAL, "不等于"},
            {ConditionRelations.GREATER, "大于"},
            {ConditionRelations.LESS, "小于"},
            {ConditionRelations.EOG, "大于等于"},
            {ConditionRelations.EOL, "小于等于"},
            {ConditionRelations.CONTAINS, "包含"},
            {ConditionRelations.NOT_CONTAIONS, "不包含"},
            {ConditionRelations.IS_TRUE, "等于True"},
            {ConditionRelations.IS_FALSE, "等于False"},
            {ConditionRelations.IS_NULL, "等于Null"},
            {ConditionRelations.NOT_NULL, "不等于Null"},
            {ConditionRelations.IS_EMPTY_STRING, "等于空字符串"},
            {ConditionRelations.NOT_EMPTY_STRING, "不等于空字符串"},
            {ConditionRelations.START_WITH, "开头等于"},
            {ConditionRelations.END_WITH, "结尾等于"},
            {ConditionRelations.NOT_START_WITH, "开头不等于"},
            {ConditionRelations.NOT_END_WITH, "结尾不等于"}
        };
        public static HashSet<ConditionRelations> singleRelation = new HashSet<ConditionRelations>() {
            {ConditionRelations.IS_TRUE },
            {ConditionRelations.IS_FALSE },
            {ConditionRelations.IS_NULL },
            {ConditionRelations.NOT_NULL },
            {ConditionRelations.IS_EMPTY_STRING },
            {ConditionRelations.NOT_EMPTY_STRING }
        };
        private string con1;
        private string con2;
        private ConditionRelations conRelation = ConditionRelations.EQUAL;

        public IFCondition(string con1, string con2, ConditionRelations conRelation) {
            this.con1 = con1;
            this.con2 = con2;
            this.conRelation = conRelation;
        }
        public IFCondition() {
           
        }

        public string Con1{ 
            get => con1;
            set => con1 = value;
        }
        public string Con2{ 
            get => con2;
            set => con2 = value;
        }

        public ConditionRelations ConRelation{ 
            get => conRelation;
            set => conRelation = value;
        }

        public bool GetConResult([Optional] ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData) {
            string con1temp = this.con1;
            string con2temp = this.con2;
            if(globleData != null && !this.ConRelation.Equals(ConditionRelations.IS_NULL)
                && !this.ConRelation.Equals(ConditionRelations.NOT_NULL)) { 
                this.GetStringVar(ref con1temp, globleData);
                this.GetStringVar(ref con2temp, globleData);
            }
            int intcon1 = 0;
            int intcon2 = 0;
            switch(this.conRelation) {
                case ConditionRelations.EQUAL:
                    return con1temp.Equals(con2temp);
                 case ConditionRelations.NOT_EQUAL:
                    return !con1temp.Equals(con2temp);
                 case ConditionRelations.GREATER:
                    if(!int.TryParse(con1temp, out intcon1)) { 
                        throw new RpaRuntimeException($"条件{this.con1}不是数字无法进行比较");
                    }
                    if(!int.TryParse(con2temp, out intcon2)) { 
                        throw new RpaRuntimeException($"条件{this.con2}不是数字无法进行比较");
                    }
                    return intcon1 > intcon2;
                 case ConditionRelations.LESS:
                    if(!int.TryParse(con1temp, out intcon1)) { 
                        throw new RpaRuntimeException($"条件{this.con1}不是数字无法进行比较");
                    }
                    if(!int.TryParse(con2temp, out intcon2)) { 
                        throw new RpaRuntimeException($"条件{this.con2}不是数字无法进行比较");
                    }
                    return intcon1 < intcon2;
                 case ConditionRelations.EOG:
                    if(!int.TryParse(con1temp, out intcon1)) { 
                        throw new RpaRuntimeException($"条件{this.con1}不是数字无法进行比较");
                    }
                    if(!int.TryParse(con2temp, out intcon2)) { 
                        throw new RpaRuntimeException($"条件{this.con2}不是数字无法进行比较");
                    }
                    return intcon1 >= intcon2;
                 case ConditionRelations.EOL:
                    if(!int.TryParse(con1temp, out intcon1)) { 
                        throw new RpaRuntimeException($"条件{this.con1}不是数字无法进行比较");
                    }
                    if(!int.TryParse(con2temp, out intcon2)) { 
                        throw new RpaRuntimeException($"条件{this.con2}不是数字无法进行比较");
                    }
                    return intcon1 <= intcon2;
                 case ConditionRelations.CONTAINS:
                    return con1temp.Contains(con2temp);
                 case ConditionRelations.NOT_CONTAIONS:
                    return !con1temp.Contains(con2temp);
                 case ConditionRelations.IS_TRUE:
                    return con1temp.ToLower().Equals("true");
                 case ConditionRelations.IS_FALSE:
                    return con1temp.ToLower().Equals("false");
                 case ConditionRelations.IS_NULL:
                     object obj = this.GetObjVar<object>(ref con1temp, globleData);
                     return obj == null;
                 case ConditionRelations.NOT_NULL:
                     object obj1 = this.GetObjVar<object>(ref con1temp, globleData);
                     return obj1 != null;
                 case ConditionRelations.IS_EMPTY_STRING:
                    return string.IsNullOrEmpty(con1temp);
                 case ConditionRelations.NOT_EMPTY_STRING:
                    return !string.IsNullOrEmpty(con1temp);
                 case ConditionRelations.START_WITH:
                    return con1temp.StartsWith(con2temp);
                 case ConditionRelations.END_WITH:
                     return con1temp.EndsWith(con2temp);
                 case ConditionRelations.NOT_START_WITH:
                    return !con1temp.StartsWith(con2temp);
                 case ConditionRelations.NOT_END_WITH:
                     return !con1temp.EndsWith(con2temp);
                default:
                    return false;
            }
        }
    }
}
